package springboot.study.project.util.nCoV_Crawler2019;


import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Ticsmyc
 * @package fun.ticsmyc.service
 * @date 2020-01-27 15:48
 */
public class EpidemicInfoService {
    private static final Logger logger = LoggerFactory.getLogger(EpidemicInfoService.class);
    public static Map<String, Integer> epidemicInfo = new HashMap<>();
    // quanguoTrendChart 重要的外国趋势图
    // hbFeiHbTrendChart 重要的外国趋势图
    // foreignTrendChart 重要的外国趋势图
    // importantForeignTrendChart 重要的外国趋势图
    // foreignTrendChartGlobal 国外趋势图全球
    // importantForeignTrendChartGlobal 重要的外国趋势图全球
    public static List<Map<String, Object>> quanguoTrendChart = new ArrayList<>();

    static {
        epidemicInfo.put("currentConfirmedCount", 0); // 现存确诊
        epidemicInfo.put("currentConfirmedIncr", 0); // 现存确诊较昨日新增
        epidemicInfo.put("suspectedCount", 0); // 现存确诊
        epidemicInfo.put("suspectedIncr", 0); // 现存确诊较昨日新增
        epidemicInfo.put("seriousCount", 0); // 现存无症状
        epidemicInfo.put("seriousIncr", 0); // 现存无症状较昨日新增
        epidemicInfo.put("confirmedCount", 0); // 累计确诊
        epidemicInfo.put("confirmedIncr", 0); // 累计确诊较昨日新增
        epidemicInfo.put("deadCount", 0); // 累计死亡
        epidemicInfo.put("deadIncr", 0); // 累计死亡较昨日新增
        epidemicInfo.put("curedCount", 0); // 累计治愈
        epidemicInfo.put("curedIncr", 0); // 累计治愈较昨日新增
    }

    public static String getNews() {
        //获取HTML数据
        Tools.getPageByJSoup(Crawler.URL);

        //提取static信息的json数据
        String staticInformation = null;
        //解析static信息的json数据
        Statistics statisticsInformation = null;
        try {
            staticInformation = Tools.getInformation(Crawler.STATIC_INFORMATION_REGEX_TEMPLATE_1, "id", Crawler.STATIC_INFORMATION_ATTRIBUTE);
            //去掉static数据正则1匹配最后的 }catch(e){}串， 剩下的就是一个格式正确的json串了
            staticInformation = staticInformation.replace("}catch(e){}", "");
            statisticsInformation = Parse.parseStatisticsInformation(staticInformation);
        } catch (Exception e1) {
            logger.error("static信息正则1匹配失败，切换正则2");
            try {
                staticInformation = Tools.getInformation(Crawler.STATIC_INFORMATION_REGEX_TEMPLATE_2, "id", Crawler.STATIC_INFORMATION_ATTRIBUTE);
                statisticsInformation = Parse.parseStatisticsInformation(staticInformation);
            } catch (Exception e2) {
                logger.error("static信息正则2匹配失败，切换正则3");
                staticInformation = Tools.getInformation(Crawler.STATIC_INFORMATION_REGEX_TEMPLATE_3, "id", Crawler.STATIC_INFORMATION_ATTRIBUTE);
                statisticsInformation = Parse.parseStatisticsInformation(staticInformation);
            }
        } finally {
            Map<String, Object> staticInformationMap = (Map) JSON.parse(staticInformation);
            if (staticInformationMap != null && staticInformationMap.containsKey("quanguoTrendChart")) {
                quanguoTrendChart = (List) staticInformationMap.get("quanguoTrendChart");
                for (Map<String, Object> map : quanguoTrendChart){
                    System.out.print(map.get("title")+" : ");
                    System.out.println(map.get("imgUrl"));
                }
            }
        }

        //if (statisticsNews != null) {
        //总数据发生变化，各省数据更新
        //提取其他信息的json数据
        String timelineServiceInformation = Tools.getInformation(Crawler.TIME_LINE_REGEX_TEMPLATE, "id", Crawler.TIME_LINE_ATTRIBUTE);
        String areaInformation = Tools.getInformation(Crawler.AREA_INFORMATION_REGEX_TEMPLATE, "id", Crawler.AREA_INFORMATION_ATTRIBUTE);
        //解析
        List<TimeLine> timeLineList = Parse.parseTimeLineInformation(timelineServiceInformation);
        List<AreaStat> areaStatList = Parse.parseAreaInformation(areaInformation);


        // 更新全局疫情数据
        epidemicInfo.put("currentConfirmedCount", statisticsInformation.getCurrentConfirmedCount()); // 现存确诊
        epidemicInfo.put("currentConfirmedIncr", statisticsInformation.getCurrentConfirmedIncr()); // 现存确诊较昨日新增
        epidemicInfo.put("suspectedCount", statisticsInformation.getSuspectedCount()); // 境外输入
        epidemicInfo.put("suspectedIncr", statisticsInformation.getSuspectedIncr()); // 境外输入较昨日新增
        epidemicInfo.put("seriousCount", statisticsInformation.getSeriousCount()); // 现存无症状
        epidemicInfo.put("seriousIncr", statisticsInformation.getSeriousIncr()); // 现存无症状较昨日新增
        epidemicInfo.put("confirmedCount", statisticsInformation.getConfirmedCount()); // 累计确诊
        epidemicInfo.put("confirmedIncr", statisticsInformation.getConfirmedIncr()); // 累计确诊较昨日新增
        epidemicInfo.put("deadCount", statisticsInformation.getDeadCount()); // 累计死亡
        epidemicInfo.put("deadIncr", statisticsInformation.getDeadIncr()); // 累计死亡较昨日新增
        epidemicInfo.put("curedCount", statisticsInformation.getCuredCount()); // 累计治愈
        epidemicInfo.put("curedIncr", statisticsInformation.getCuredIncr()); // 累计治愈较昨日新增

        StringBuffer result = new StringBuffer();
        result.append("现存确诊:").append(statisticsInformation.getCurrentConfirmedCount()).append("\r\n");
        result.append("现存确诊较昨日新增:").append(statisticsInformation.getCurrentConfirmedIncr()).append("\r\n");
        result.append("境外输入:").append(statisticsInformation.getSuspectedCount()).append("\r\n");
        result.append("境外输入较昨日新增:").append(statisticsInformation.getSuspectedIncr()).append("\r\n");
        result.append("现存无症状:").append(statisticsInformation.getSeriousCount()).append("\r\n");
        result.append("现存无症状较昨日新增:").append(statisticsInformation.getSeriousIncr()).append("\r\n");
        result.append("累计确诊:").append(statisticsInformation.getConfirmedCount()).append("\r\n");
        result.append("累计确诊较昨日新增:").append(statisticsInformation.getConfirmedIncr()).append("\r\n");
        result.append("累计死亡:").append(statisticsInformation.getDeadCount()).append("\r\n");
        result.append("累计死亡较昨日新增:").append(statisticsInformation.getDeadIncr()).append("\r\n");
        result.append("累计治愈:").append(statisticsInformation.getCuredCount()).append("\r\n");
        result.append("累计治愈较昨日新增:").append(statisticsInformation.getCuredIncr()).append("\r\n");

        return result.toString();
    }

    public static void main(String[] args) {
        System.out.println(getNews());
    }
}
